package atmGUI.dao.impl;

import atmGUI.dao.BaseDAO;
import atmGUI.dao.UserDAO;
import atmGUI.pojo.UserDO;

import java.sql.SQLException;

public class UserDAOImpl extends BaseDAO implements UserDAO {
    @Override
    public UserDO getByUserAccountAndPassword(String userAccount, String password) {
        this.getConnection();
        statement = null;
        rs = null;
        UserDO userDO = null;
        String sql = "select * from t_user where u_account=? and u_password=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, userAccount);
            statement.setObject(2, password);
            rs = statement.executeQuery();
            if (rs.next()) {
                userDO = new UserDO();
                userDO.setId(rs.getInt("u_id"));
                userDO.setAccount(rs.getString("u_account"));
                userDO.setPassword(rs.getString("u_password"));
                userDO.setBalance(rs.getDouble("u_balance"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement, rs);
        }
        return userDO;
    }

    @Override
    public UserDO getById(int id) {
        this.getConnection();
        statement = null;
        rs = null;
        UserDO userDO = null;
        String sql = "select * from t_user where u_id=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, id);
            rs = statement.executeQuery();
            if (rs.next()) {
                userDO = new UserDO();
                userDO.setId(rs.getInt("u_id"));
                userDO.setAccount(rs.getString("u_account"));
                userDO.setPassword(rs.getString("u_password"));
                userDO.setBalance(rs.getDouble("u_balance"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement, rs);
        }
        return userDO;
    }

    @Override
    public void update(UserDO userDO) {
        this.getConnection();
        String sql = "update t_user set u_password=?,u_balance=? where u_id=?";
        try {
            // 2. 获取执行SQL语句对象
            statement = con.prepareStatement(sql);
            statement.setObject(1, userDO.getPassword());
            statement.setObject(2, userDO.getBalance());
            statement.setObject(3, userDO.getId());
            // 3. 执行SQL语句
            int row = statement.executeUpdate();
            System.out.println("受影响的行数：" + row);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            // 4. 关闭连接
            close(con, statement);
        }
    }

    @Override
    public UserDO getByUserAccount(String account) {
        this.getConnection();
        statement = null;
        rs = null;
        UserDO userDO = null;
        String sql = "select * from t_user where u_account=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, account);
            rs = statement.executeQuery();
            if (rs.next()) {
                userDO = new UserDO();
                userDO.setId(rs.getInt("u_id"));
                userDO.setAccount(rs.getString("u_account"));
                userDO.setPassword(rs.getString("u_password"));
                userDO.setBalance(rs.getDouble("u_balance"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement, rs);
        }
        return userDO;
    }
}
